package com.shuda.business.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuda.business.user.entity.UserEntity;
import com.shuda.business.user.service.UserService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.result.Result;
import com.shuda.common.utils.PageResult;
import com.shuda.common.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 用户管理控制器类
 */
@RestController
@RequestMapping("/api/system/user")
@Tag(name = "系统用户管理", description = "系统用户管理相关接口")
@RequiredArgsConstructor
@Validated
public class UserController {

    private final UserService userService;

    /**
     * 分页查询用户列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询用户列表")
    public Result<PageResult<UserEntity>> getUserPage(@Valid PageDTO pageDTO, 
                                                       @RequestParam(required = false) String username,
                                                       @RequestParam(required = false) String nickname,
                                                       @RequestParam(required = false) String status) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        if (username != null && !username.trim().isEmpty()) {
            queryWrapper.like("username", username);
        }
        if (nickname != null && !nickname.trim().isEmpty()) {
            queryWrapper.like("nickname", nickname);
        }
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByDesc("create_time");
        
        PageResult<UserEntity> pageResult = userService.getUserPage(pageDTO, queryWrapper);
        return Result.success(pageResult);
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取用户详情")
    public Result<UserEntity> getUserById(@PathVariable Long id) {
        UserEntity user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 创建用户
     */
    @PostMapping
    @Operation(summary = "创建用户")
    public Result<String> createUser(@Valid @RequestBody UserEntity user) {
        userService.createUser(user);
        return Result.success("用户创建成功");
    }

    /**
     * 更新用户
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新用户")
    public Result<String> updateUser(@PathVariable Long id, @Valid @RequestBody UserEntity user) {
        user.setId(id);
        userService.updateUser(user);
        return Result.success("用户更新成功");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除用户")
    public Result<String> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success("用户删除成功");
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除用户")
    public Result<String> deleteUsers(@RequestBody List<Long> ids) {
        userService.deleteUsers(ids);
        return Result.success("用户批量删除成功");
    }

    /**
     * 更新用户状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新用户状态")
    public Result<String> updateUserStatus(@PathVariable Long id, @RequestParam String status) {
        userService.updateUserStatus(id, status);
        return Result.success("用户状态更新成功");
    }

    /**
     * 重置用户密码
     */
    @PutMapping("/{id}/password/reset")
    @Operation(summary = "重置用户密码")
    public Result<String> resetPassword(@PathVariable Long id, @RequestParam String password) {
        userService.resetPassword(id, password);
        return Result.success("密码重置成功");
    }

    /**
     * 分配用户角色
     */
    @PutMapping("/{id}/roles")
    @Operation(summary = "分配用户角色")
    public Result<String> assignRoles(@PathVariable Long id, @RequestBody List<Long> roleIds) {
        userService.assignRoles(id, roleIds);
        return Result.success("角色分配成功");
    }

    /**
     * 获取用户角色
     */
    @GetMapping("/{id}/roles")
    @Operation(summary = "获取用户角色")
    public Result<List<Long>> getUserRoles(@PathVariable Long id) {
        List<Long> roleIds = userService.getUserRoles(id);
        return Result.success(roleIds);
    }
}